home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / GRAPH_FO / (GRAPH / CGRAPHTE / CGRAPHDO.C < prev    next >
Text File  |  1991-02-15  |  9KB  |  255 lines

  1. /******************************************************************************
  2.  CGraphDoc.c
  3.  
  4.                             The GraphDoc Class
  5.  
  6.     SUPERCLASS = CDocument.c
  7.  
  8.     Copyright ⌐ 1989 Symantec Corporation. All rights reserved.
  9.  
  10.  ******************************************************************************/
  11.  
  12. #include <Global.h>
  13. #include <Commands.h>
  14. #include <CBartender.h>
  15. #include <CDesktop.h>
  16. #include <CDecorator.h>
  17. #include <CScrollPane.h>
  18. #include <CPanorama.h>
  19. #include <CError.h>
  20. #include <TBUtilities.h>
  21. #include "CGraphDoc.h"
  22. #include "CGraphPane.h"
  23.  
  24. extern CBartender    *gBartender;        /* Manages all menus                */
  25. extern OSType        gSignature;            /* Creator for Application's files    */
  26. extern CDesktop        *gDesktop;            /* The visible Desktop                */
  27. extern CDecorator    *gDecorator;        /* Decorator for arranging windows    */
  28. extern CError        *gError;            /* global Error handler */
  29.  
  30. #define        WIND_Graph        2000    /* WIND resource ID                    */
  31.  
  32.  
  33. /******************************************************************************
  34.  IGraphDoc
  35.  
  36.         Initialize a GraphDoc object
  37.  ******************************************************************************/
  38.  
  39. void    CGraphDoc::IGraphDoc(
  40.     CApplication        *itsSupervisor)
  41. {
  42.     CDocument::IDocument(itsSupervisor, TRUE);
  43. }
  44.  
  45.  
  46. /******************************************************************************
  47.  NewFile {OVERRIDE}
  48.  
  49.         Set up a document with a new file, usually in response the "New"
  50.         command in the File menu.
  51.  ******************************************************************************/
  52.  
  53. void    CGraphDoc::NewFile()
  54. {
  55.     Str255        wTitle;                    /* Window title string                */
  56.     short        wCount;                    /* Index number of new window        */
  57.     Str63        wNumber;                /* Index number as a string            */
  58.  
  59.         /* &&& Set up a new document, including an associated window    */
  60.  
  61.     BuildWindow(NULL);                    /* XXX Here we use a BuildWindow()    */
  62.                                         /*   method with a NULL parameter.    */
  63.                                         /*   Usually, the OpenFile() method    */
  64.                                         /*   (see below), will also send a    */
  65.                                         /*   BuildWindow() message, but        */
  66.                                         /*   with a handle to some data.    */
  67.  
  68.     itsWindow->GetTitle(wTitle);        /* Append an index number to the    */
  69.     wCount = gDecorator->GetWCount();    /*   default name of the window        */
  70.     NumToString((long)wCount, wNumber);
  71.     ConcatPStrings(wTitle, (StringPtr) "\p-");
  72.     ConcatPStrings(wTitle, wNumber);
  73.     itsWindow->SetTitle(wTitle);
  74.  
  75.     itsWindow->Select();                /* Make it the active window        */
  76. }
  77.  
  78.  
  79. /******************************************************************************
  80.  OpenFile {OVERRIDE}
  81.  
  82.         Open a file for a document, usually in response the "Open╔"
  83.         command in the File menu.
  84.  ******************************************************************************/
  85.  
  86. void    CGraphDoc::OpenFile(
  87.     SFReply        *macSFReply)
  88. {
  89.  
  90.         /* &&& Create and initialize a new file object */
  91.  
  92.     itsFile = new(CFile);                /* XXX Create a new file object        */
  93.     ((CFile*)itsFile)->IFile();            /* XXX Initialize file object. Type    */
  94.                                         /*   casting will be necessary.        */
  95.  
  96.     itsFile->Open(fsRdWrPerm);            /* Open file with read/write access    */
  97.  
  98.  
  99.         /* &&& Read data from the file and set up the document using    */
  100.         /* this data. Also create a window for displaying the data.        */
  101.  
  102.     BuildWindow(NULL);                    /* XXX Send BuildWindow() message,    */
  103.                                         /*   passing parameters which        */
  104.                                         /*   depend on the data read from    */
  105.                                         /*   the file.                        */
  106.  
  107.                                         /* Set window title to the name of    */
  108.                                         /*   the file                        */
  109.     itsWindow->SetTitle(macSFReply->fName);
  110.  
  111.     itsWindow->Select();                /* Make this the active window        */
  112. }
  113.  
  114.  
  115. /******************************************************************************
  116.  BuildWindow {XXX}
  117.  
  118.         Build a window for a document. This is NOT an overridden method,
  119.         but rather an example of how you might structure your document
  120.         class. The NewFile() and OpenFile() methods both require setting
  121.         up a new document. NewFile() creates an empty document, possibly
  122.         using some default data, whereas OpenFile() creates a document
  123.         based on the information read from a file. Both methods can invoke
  124.         the BuildWindow(), passing the information needed to set up a
  125.         document. BuildWindow() takes care of the common stuff: Creating
  126.         a window, and creating and arranging panes within the window.
  127.  ******************************************************************************/
  128.  
  129. void    CGraphDoc::BuildWindow(
  130.     Handle    theData)                    /* XXX Application-specific data    */
  131. {
  132.     CScrollPane        *theScrollPane;
  133.     Rect            sizeRect;
  134.  
  135.         /* &&& Create a window and the panes which go inside it    */
  136.  
  137.     itsWindow = new(CWindow);            /* Create a non-floating window        */
  138.                                         /*   using a WIND resource template    */
  139.     itsWindow->IWindow(WIND_Graph, FALSE, gDesktop, this);
  140.  
  141.                                         /* XXX Specify maximum and minimum    */
  142.                                         /*   dimensions of our window        */
  143.     gDesktop->GetBounds(&sizeRect);
  144.     InsetRect(&sizeRect, 5, 5);
  145.  
  146.     sizeRect.top =  sizeRect.left = 100;
  147.     itsWindow->SetSizeRect(&sizeRect);
  148.  
  149.                                         /* Decorator staggers windows on    */
  150.                                         /*   the screen                        */
  151.     gDecorator->PlaceNewWindow(itsWindow);
  152.  
  153.                                         /* Create a ScrollPane in the window */
  154.     theScrollPane = new(CScrollPane);
  155.                                         /* Use a resource template to set    */
  156.                                         /*   parameters of the ScrollPane    */
  157.     theScrollPane->IViewRes('ScPn', 1, itsWindow, this);
  158.                                         /* Make ScrollPane fit snuggly to    */
  159.                                         /*   the frame of the window        */
  160.     theScrollPane->FitToEnclFrame(TRUE, TRUE);
  161.  
  162.                                         /* XXX Put our GraphPane inside    */
  163.                                         /*   the ScrollPane                    */
  164.     itsMainPane = new(CGraphPane);
  165.     ((CGraphPane*)itsMainPane)->IViewRes('GrPn', 128, theScrollPane, this);
  166.  
  167.                                         /* Our Panorama fits inside the        */
  168.                                         /*   the ScrollPane                    */
  169.     itsMainPane->FitToEnclosure(TRUE, TRUE);
  170.     theScrollPane->InstallPanorama((CPanorama*)itsMainPane);
  171.  
  172.  
  173.     itsGopher = itsMainPane;            /* XXX GraphPane may want to be    */
  174.                                         /*   the Gopher when Document is    */
  175.                                         /*   active so it can directly        */
  176.                                         /*   receive key and menu commands    */
  177. }
  178.  
  179.  
  180. /******************************************************************************
  181.  DoSave {OVERRIDE}
  182.  
  183.         Save a file under the same name. Return TRUE if save was successful.
  184.  ******************************************************************************/
  185.  
  186. Boolean    CGraphDoc::DoSave()
  187. {
  188.     gError->PostAlert(131, 1);
  189. }
  190.  
  191. /******************************************************************************
  192.  DoSaveAs {OVERRIDE}
  193.  
  194.         Save a file under another name. Return TRUE if save was successful.
  195.         Note that this message is sent by the DoSaveFileAs() method after
  196.         the user picks a new name and confirms the save.
  197.  ******************************************************************************/
  198.  
  199. Boolean    CGraphDoc::DoSaveAs(
  200.     SFReply        *macSFReply)            /* Standard File reply record        */
  201. {
  202.     if (itsFile != NULL) {                /* If a file object already exists,    */
  203.         itsFile->Dispose();                /*   throw it out. This will also    */
  204.     }                                    /*   close the file.                */
  205.  
  206.         /* &&& Create and initialize a new file object */
  207.  
  208.     itsFile = new(CFile);                /* XXX Create a new file object        */
  209.     ((CFile*)itsFile)->IFile();            /* XXX Initialize file object. Type    */
  210.                                         /*   casting will be necessary.        */
  211.  
  212.     itsFile->SFSpecify(macSFReply);        /* Specify file parameters            */
  213.  
  214.                                         /* XXX Create a new file on disk    */
  215.                                         /*   with desired creator and type.    */
  216.                                         /*   You'll want to change 'PNTG'    */
  217.                                         /*   to the file type your program    */
  218.                                         /*   creates.                        */
  219.     itsFile->CreateNew(gSignature, 'PNTG');
  220.  
  221.     itsFile->Open(fsRdWrPerm);            /* Open file with read/write access    */
  222.  
  223.                                         /* Change name of window to match    */
  224.                                         /*   new file name                    */
  225.     itsWindow->SetTitle(macSFReply->fName);
  226.  
  227.     return( DoSave() );                    /* Save contents to disk            */
  228. }
  229.  
  230.  
  231. /******************************************************************************
  232.  DoRevert {OVERRIDE}
  233.  
  234.         Throw out the current contents of a document and revert to the
  235.         last saved version.
  236.  ******************************************************************************/
  237.  
  238. void    CGraphDoc::DoRevert()
  239. {
  240.     Point    homePos;                    /* Home position of a Panorama        */
  241.  
  242.         /* &&& Dispose of current (in memory) contents of the document    */
  243.         /* and read the information back from the file on disk.            */
  244.  
  245.                                         /* XXX If you use Panoramas,        */
  246.                                         /* remember to reset them back to    */
  247.                                         /* the home position                */
  248.     ((CPanorama*)itsMainPane)->GetHomePosition(&homePos);
  249.     ((CPanorama*)itsMainPane)->ScrollTo(homePos, FALSE);
  250.  
  251.     itsMainPane->Refresh();                /* Force update of contents            */
  252.  
  253.     dirty = FALSE;                        /* Reverts to a clean document        */
  254. }
  255.